package com.M10.BE.inventory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.M10.BE.R;
import com.M10.BE.adapter.NNewAdapter;
import com.M10.BE.adapter.NullStringToEmptyAdapterFactory;
import com.M10.BE.database.TableAsset;
import com.M10.BE.database.TableInventory;
import com.M10.BE.entity.DownInventoryDetail;
import com.M10.BE.entity.InventoryDetail;
import com.M10.BE.entity.NAsset;
import com.fn.useries.UHFApplication;
import com.fn.useries.utils.PreferenceUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2018/8/13.
 */

public class UncheckedInveDetailActivity extends Activity {
    public static final String TAG = "UncheckedInveDetailActivity";
    @BindView(R.id.tv_inve_back)
    TextView tv_back;
    @BindView(R.id.tv_inve_orderid)
    TextView tv_inve_orderid;
    @BindView(R.id.tv_inve_dept)
    TextView tv_inve_dept;
    @BindView(R.id.tv_inve_producttype)
    TextView tv_inve_producttype;
    @BindView(R.id.tv_inve_labeltype)
    TextView tv_inve_labeltype;
    @BindView(R.id.tv_inve_setime)
    TextView tv_inve_setime;
    @BindView(R.id.tv_inve_jine)
    TextView tv_inve_jine;
    @BindView(R.id.tv_inve_total)
    TextView tv_inve_total;
    @BindView(R.id.tv_inve_checkednum)
    TextView tv_inve_checkednum;
    @BindView(R.id.lv_inventory)
    ListView lv_inventory;
    @BindView(R.id.tv_download)
    TextView tv_down;
    String inventoryID;
    Gson gson;
    InventoryDetail inventoryDetail = null;
    NNewAdapter nNewAdapter;
    List<NAsset> list;
    private Context context;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inve_detail_uncheck);
        ButterKnife.bind(this);
        context = UncheckedInveDetailActivity.this;
        inventoryID = getIntent().getStringExtra("inventoryId");
        gson = new GsonBuilder().serializeNulls().
                registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
        nNewAdapter = new NNewAdapter(UncheckedInveDetailActivity.this, list, lv_inventory);
        lv_inventory.setAdapter(nNewAdapter);
        getDetail(inventoryID);
    }

    @OnClick(R.id.tv_inve_back)

    public void onViewClicked() {
        finish();
    }


    @OnClick(R.id.tv_download)
    public void down() {
        String a = PreferenceUtils.readString(UncheckedInveDetailActivity.this, "assets", "downinventory");
        if (a != null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(UncheckedInveDetailActivity.this);
            builder.setMessage("你有一个未完成盘点的任务，是否进入继续操作");
            builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(UncheckedInveDetailActivity.this, DoInventoryActivity.class);
                    startActivity(intent);
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
        } else {
            downInveList(inventoryID);
        }

    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void getDetail(String inventoryID) {
        OkGo.<String>get(UHFApplication.BASE_HEAD + "judge/APPShowUI")
                .tag(this)
                .params("id", inventoryID)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject object = new JSONObject(response.body());
                            String errcode = object.getString("statusCode");
                            String msg = object.getString("msg");
                            if ("0".equals(errcode)) {
                                String result = object.getString("result");
                                inventoryDetail = gson.fromJson(result, InventoryDetail.class);
                                Log.e(TAG, object.toString());
                                setViewValue(inventoryDetail);
                            } else {
                                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                    }
                });
    }

    //APPDownLoad/APPShowUI
    public void downInveList(String inventoryID) {
        OkGo.<String>get(UHFApplication.BASE_HEAD + "judge/APPDownLoad")
                .tag(this)
                .params("id", inventoryID)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject object = new JSONObject(response.body());
                            String errcode = object.getString("statusCode");
                            String msg = object.getString("msg");
                            if ("0".equals(errcode)) {
                                String result = object.getString("result");
                                DownInventoryDetail downInventoryDetail = gson.fromJson(result, DownInventoryDetail.class);
                                if (TableInventory.insertInventory(context, downInventoryDetail)) {
                                    if (downInventoryDetail.getList() != null) {
                                        for (int i = 0; i < downInventoryDetail.getList().size(); i++) {
                                            TableAsset.insertAsset(context, downInventoryDetail.getList().get(i));
                                        }
                                    }
                                }
                                //下载成功，去盘点
                                Intent intent = new Intent(context, DoInventoryActivity.class);
                                startActivity(intent);
                            } else {
                                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                    }
                });
    }

    private void setViewValue(InventoryDetail inventoryDetail) {
        tv_inve_orderid.setText(inventoryDetail.getInventoryID());
        tv_inve_dept.setText(inventoryDetail.getQueryOrgName());
        tv_inve_producttype.setText(inventoryDetail.getCzTypeName());
        tv_inve_labeltype.setText(inventoryDetail.getQueryLabelType());
        tv_inve_setime.setText(inventoryDetail.getQuerySTime() + "-" + inventoryDetail.getQueryETime());
        tv_inve_jine.setText(inventoryDetail.getQuerySAmount() + "~" + inventoryDetail.getQueryEAmount());
        tv_inve_total.setText(inventoryDetail.getQueryCount());
        tv_inve_checkednum.setText(inventoryDetail.getJudgeCount());
        nNewAdapter.setList(inventoryDetail.getZclist());
        nNewAdapter.notifyDataSetChanged();
    }
}
